package com.example.qxfw.backgroundManagement.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.qxfw.common.entity.UserProduct;
import com.example.qxfw.common.entity.UserTable;
import com.example.qxfw.rcyw.pojo.JobMake;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

/************************************
 *
 * @author : HuangDeYin Jolly
 * @since : JDK 11
 * @Date : 2023/6/30  星期五   15:54
 * @description : 此类用来 产品管理
 ***************************************/
@Mapper
@DS("service")
public interface ProductManagerMapper {

    @Select("select id, username " +
            "from user_table")
    List<UserTable> selectIdAndUserName();

    @Select("select distinct producttype " +
            "from user_product")
    List<UserProduct> selectProductType();

    @Select("select distinct jobtype from job_make")
    List<JobMake> selectJobType();

    @Select("select id from user_table")
    List<Integer> selectIds();

    @Select("<script> " +
            "select id, userid, producttype, productname, tel, email, ftpaddress, wechat, faxnumber, recentmaketime, recentdistributetime, productprice, buytime, expirytime " +
            "from user_product " +
            "where id in " +
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'>" +
            " '${id}' " +
            "</foreach> " +
            "<if test='startTime!=null and endTime!=null'>and buytime between '${startTime}' and '${endTime}'</if> " +
            "<if test='startTime!=null and endTime==null'>and buytime &gt; '${startTime}'</if> " +
            "<if test='startTime==null and endTime!=null'>and buytime &lt; '${endTime}'</if> " +
            "and producttype like concat('%','${productType}','%') " +
            "and (productname like concat('%','${productName}','%') or productname is null)" +
            "</script>")
    List<UserProduct> selectUserProductByUserId(@Param("ids") List<Integer> ids, @Param("startTime") Timestamp startTime, @Param("endTime") Timestamp endTime, @Param("productType") String productType, @Param("productName") String productName);


    @Select("select id from user_product order by id desc limit 1")
    Integer selectIdMax();


    @Insert("insert into user_product(id, userid, producttype, productname, tel, email, ftpaddress, wechat, faxnumber, recentmaketime, recentdistributetime, productprice, buytime, expirytime) " +
            "VALUES (#{id},#{userProduct.userid},#{userProduct.producttype},#{userProduct.productname},#{userProduct.tel},#{userProduct.email},#{userProduct.ftpaddress}#{userProduct.wechat},#{userProduct.faxnumber},#{userProduct.recentmaketime},#{userProduct.recentdistributetime},#{userProduct.productprice},#{userProduct.buytime},#{userProduct.expirytime})")
    Integer saveUserProduct(@Param("id") int id, @Param("userProduct") UserProduct userProduct);

    @Update("update user_product " +
            "set producttype=#{producttype},productname=#{productname},tel=#{tel},email=#{email},ftpaddress=#{ftpaddress},wechat=#{wechat},faxnumber=#{faxnumber},productprice=#{productprice},buytime=#{buytime},expirytime=#{expirytime} " +
            "where id = #{id}")
    Integer updateUserProduct(UserProduct userProduct);

    @Delete("delete from user_product where id = #{id}")
    Integer deleteUserProduct(int id);

    @Select("<script> " +
            "select id, userid, producttype, productname, tel, email, ftpaddress, wechat, faxnumber, recentmaketime, recentdistributetime, productprice, buytime, expirytime " +
            "from user_product " +
            "where id in " +
            "<foreach item='id' collection='ids' open='(' separator=',' close=')'> " +
            "#{id} " +
            "</foreach>" +
            "</script> ")
    List<UserProduct> exportUserProduct(@Param("ids") List<Integer> ids);

    @Select("truncate table user_product")
    Integer clearUserProduct();

    @Select("select productname from user_product where producttype = #{productType}")
    List<UserProduct> selectProductName(String productType);

    @Select("select jobname from job_make where jobtype = #{jobType}")
    List<JobMake> selectJobName(String jobType);
}
